package com.yj.suncity.publish;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yj.suncity.R;
import com.yj.suncity.utis.ToastUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;

public class PicSelectActivity extends Activity implements OnItemClickListener{
	protected static final String TAG = "PicSelectActivity";

	private GridView gridView;
	private Button mBtnOK;
	private ArrayList<String> picPaths = new ArrayList<String>();
	private Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
	private int width;
	private int picPathSize;
	private DisplayImageOptions options; // 显示图片的设置
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pic_select);
		context=this;
		width = getResources().getDisplayMetrics().widthPixels;
		picPathSize = getIntent().getIntExtra("picPathSize", 0);
		findView();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.android_layout_detail)
				.showImageForEmptyUri(R.drawable.android_layout_detail)
				.showImageOnFail(R.drawable.android_layout_detail)
				.cacheInMemory(true).cacheOnDisc(true)
				.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
				.build();

		getImagPic(this);

	}

	private void findView() {
	
		mBtnOK = (Button) findViewById(R.id.btn_ok);

		gridView = (GridView) findViewById(R.id.gridView1);
		gridView.setOnItemClickListener(this);
	}

	private void getImagPic(Context context) {

		final List<String> imgPaths = getImagPath(context);
		if (imgPaths == null) {
			ToastUtil.ShowToast(context, "您还没有更多图片！");
			return;
		}

		gridView.setAdapter(new ImageAdapter(context, imgPaths));

	}

	class ImageAdapter extends BaseAdapter {
		private List<String> imgPaths;
		private ImageLoader imageLoader;
		private Context context;

		public ImageAdapter(Context context, List<String> imgPaths) {
			super();
			this.imgPaths = imgPaths;
			this.context = context;
			imageLoader = ImageLoader.getInstance();
			initData();
		}

		@Override
		public int getCount() {
			return imgPaths.size();
		}

		private void initData() {
			for (int i = 0; i < imgPaths.size(); i++) {
				map.put(i, false);
			}
		}

		@Override
		public Object getItem(int position) {
			return imgPaths.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = inflater.inflate(R.layout.pic_select_adapter,
						null);
				viewHolder.imageView = (ImageView) convertView
						.findViewById(R.id.imageView1);
				viewHolder.imageView
						.setLayoutParams(new RelativeLayout.LayoutParams(
								width / 3, width / 3));

				viewHolder.is_select = (CheckBox) convertView
						.findViewById(R.id.is_show);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			imageLoader.displayImage("file://" + imgPaths.get(position),
					viewHolder.imageView, options);
			viewHolder.is_select.setChecked(map.get(position));
			return convertView;
		}

	}

	class ViewHolder {
		ImageView imageView;
		CheckBox is_select;
	}

	// 初始化每个Item的选中状态

	private List<String> getImagPath(Context context) {

		String SDState = Environment.getExternalStorageState();
		if (!SDState.equals(Environment.MEDIA_MOUNTED)) {
			ToastUtil.ShowToast(context, "内存卡不存在！");
			return null;
		}
		List<String> list = new ArrayList<String>();// 存放图片的路径
		// 查询媒体数据库
		String[] pojo = { MediaStore.Images.Media.DATA };
		Cursor cursor = context.getContentResolver().query(
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				pojo, null, null, MediaStore.Images.Media.DATE_MODIFIED);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				int columnIndex = cursor.getColumnIndex(pojo[0]);
				if (columnIndex != -1) {
					String picPath = cursor.getString(columnIndex);
					list.add(picPath);
					cursor.moveToNext();
				}
			}
			cursor.close();
			Collections.reverse(list);
			return list;
		}
		return null;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {
		String picPath = (String) gridView.getAdapter().getItem(position);
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.is_select.toggle();// 点击Item时，改变复选框的选中状态
		map.put(position, holder.is_select.isChecked());
		if (holder.is_select.isChecked()) {
			picPaths.add(picPath);
			mBtnOK.setText("完成(" + picPaths.size() + ")");

		} else {
			picPaths.remove(picPath);
			mBtnOK.setText("完成(" + picPaths.size() + ")");
			if (picPaths.size() == 0) {
				mBtnOK.setText("完成");
			}
		}

		if (picPaths.size() == 0) {
			mBtnOK.setClickable(false);
		} else {
			mBtnOK.setClickable(true);
		}

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ok:
			for (String picPath : picPaths) {
				Log.d("", "picPath==="+picPath);
			}
			if (picPaths.size() + picPathSize > 9) {
				ToastUtil.ShowToast(context, "您最多可以选择9张图片！");
			} else {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("picPaths", picPaths);
				intent.putExtras(bundle);
				setResult(105, intent);
				PicSelectActivity.this.finish();
			}
			break;
		case R.id.btnHeadBack:
			this.finish();
			break;
		default:
			break;
		}

	}

}
